Branchwell

Lumen & Gesso / website · lumengesso.com / app / orders / [id] / approve / page.tsx

1import Link from 'next/link';
2import { notFound, redirect } from 'next/navigation';
3import { getCustomerUser } from '@/app/lib/lumen-auth';
4import { commerceDb } from '@/app/lib/commerce-db';
5import { displayOrderId } from '@/app/lib/orders';
6import { FramedPreview } from '@/app/components/FramedPreview';
7import type { FrameId } from '@/app/data/frames';
8import type { ProductId, Orientation } from '@/app/lib/pricing';
9import { ApproveForm } from './form';
10import { OrderConversation } from '@/app/components/OrderConversation';
11import { CustomerOrderMessageForm } from '@/app/components/CustomerOrderMessageForm';
12import { orderConversation, STUDIO_MESSAGE_STATUSES } from '@/app/lib/order-messages';
13
14export const dynamic = 'force-dynamic';
15
16export default async function ApprovePage(
17 { params }: { params: Promise<{ id: string }> },
18) {
19 const { id } = await params;
20 const user = await getCustomerUser();
21 if (!user) redirect(`/login?next=/orders/${id}/approve`);
22
23 const admin = commerceDb();
24 const { data: order } = await admin
25 .from('orders')
26 .select('id, user_id, status, price_cents, current_generation_id, config, revision_note, updated_at')
27 .eq('id', id)
28 .single();
29
30 if (!order) notFound();
31 if (order.user_id !== user.id) notFound();
32
33 const cfg = (order.config ?? {}) as {
34 productId?: ProductId;
35 size?: string;
36 orientation?: Orientation;
37 frame?: string;
38 matteId?: string | null;
39 proofIsRevision?: boolean;
40 };
41
42 let proofUrl: string | null = null;
43 if (order.current_generation_id) {
44 const { data: gen } = await admin
45 .from('generations')
46 .select('clean_url')
47 .eq('id', order.current_generation_id)
48 .single();
49 proofUrl = gen?.clean_url ?? null;
50 }
51
52 const canApprove = order.status === 'awaiting_approval' && !!proofUrl;
53 const messages = orderConversation(order.config, order.revision_note, order.updated_at);
54 const canMessage = STUDIO_MESSAGE_STATUSES.includes(order.status);
55
56 return (
57 <main className="orders">
58 <div className="orders__inner" style={{ maxWidth: 880 }}>
59 <Link href={`/orders/${order.id}`} className="orders__back">← Order detail</Link>
60 <h1 className="orders__title">{displayOrderId(order.id)} · {cfg.proofIsRevision ? 'review your revised proof' : 'review your proof'}</h1>
61
62 <div className="approve">
63 {proofUrl ? (
64 <div className="approve__preview">
65 <FramedPreview
66 src={proofUrl}
67 alt="Your portrait proof in your chosen frame"
68 productId={cfg.productId ?? 'matted'}
69 size={cfg.size ?? '12x16'}
70 orientation={cfg.orientation ?? 'portrait'}
71 frame={(cfg.frame ?? 'black') as FrameId}
72 matteId={cfg.matteId ?? null}
73 />
74 <p className="approve__notice">
75 Shown here in your chosen frame &amp; size, exactly as it will be printed.
76 </p>
77 </div>
78 ) : (
79 <p className="approve__empty">No proof ready yet. We&rsquo;ll email you when your portrait is ready to review.</p>
80 )}
81
82 {(messages.length > 0 || canMessage) && (
83 <div className="approve__panel">
84 <OrderConversation messages={messages}>
85 {canMessage && <CustomerOrderMessageForm orderId={order.id} proofId={canApprove ? order.current_generation_id ?? undefined : undefined} />}
86 </OrderConversation>
87 </div>
88 )}
89
90 {canApprove ? (
91 <ApproveForm orderId={order.id} generationId={order.current_generation_id} />
92 ) : (
93 <p style={{ color: 'var(--ink-soft)', marginTop: 24 }}>
94 {order.status === 'awaiting_approval'
95 ? 'Loading proof…'
96 : order.status === 'revision_requested'
97 ? 'Approval is paused. We’ll email you when an updated proof is ready.'
98 : 'There is no proof awaiting approval.'}
99 </p>
100 )}
101 </div>
102 </div>
103 </main>
104 );
105}